package cn.vshare.service.impl;

import cn.vshare.entity.Log;
import cn.vshare.service.ExcelExportService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 日志导出excel
 */
@Service("exportExcelService")
public class ExcelExportServiceImpl implements ExcelExportService {
    @Override
    public void markWorkBook(Map<String, Object> model, Workbook workbook) {
        List<Log> logList = (List<Log>) model.get("logList");
        Sheet sheet = workbook.createSheet("所有日志");
        Row title = sheet.createRow(0);
        title.createCell(0).setCellValue("编号");
        title.createCell(1).setCellValue("操作内容");
        title.createCell(2).setCellValue("操作人昵称");
        title.createCell(3).setCellValue("操作人角色");
        title.createCell(4).setCellValue("操作客户端ip");
        title.createCell(5).setCellValue("操作时间");
        title.createCell(6).setCellValue("操作的类");
        title.createCell(7).setCellValue("操作的方法");
        title.createCell(8).setCellValue("操作人手机号");
        title.createCell(9).setCellValue("操作人邮箱");
        title.createCell(10).setCellValue("操作人注册时间");
        title.createCell(11).setCellValue("操作人性别");
        //遍历数据
        for (int i = 0; i < logList.size(); i++) {
            Log log = logList.get(i);
            Row row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(i+1);
            row.createCell(1).setCellValue(log.getMessage());
            row.createCell(2).setCellValue(log.getUser().getNickName());
            row.createCell(3).setCellValue(log.getUser().getRole().getName());
            row.createCell(4).setCellValue(log.getIp());
            row.createCell(5).setCellValue(log.getOperateTime());
            row.createCell(6).setCellValue(log.getOperateContent());
            row.createCell(7).setCellValue(log.getOperateResult());
            row.createCell(8).setCellValue(log.getUser().getPhone());
            row.createCell(9).setCellValue(log.getUser().getEmail());
            row.createCell(10).setCellValue(log.getUser().getRegisterDate());
            row.createCell(11).setCellValue(log.getUser().getSex());
        }

    }
}
